//
//  WebInfo_GetView_Tsk_c.m
//  EpointFrame5
//
//  Created by 李亚春 on 14-3-12.
//  Copyright (c) 2014年 Epoint. All rights reserved.
//

#import "WebInfo_GetView_Tsk_c.h"
#import "EpointWebInfoModel.h"
#import "EpointCacheDB.h"
#import "EpointDbConfig.h"

@implementation WebInfo_GetView_Tsk_c

-(void)execute
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSString *ValidateData = [EpointBizTools getToken];
    NSString *url = self.getMobileOAWebserviceURL;
    NSString *wsNamespace = NSLocalizedString(@"Epoint_DNet_WS_NameSpace", @"");
    NSString *userguid = [userDefaults objectForKey:Epoint_User_UserGuid];
    NSString *pagesize = [params objectForKey:@"pagesize"];
    NSString *keyword = [params objectForKey:@"keyword"];
    NSString *categoryguid = [params objectForKey:@"categoryguid"];
    NSString *CurrentPageIndex = [params objectForKey:@"CurrentPageIndex"];
    
    
    NSString *ParasXml = @"<![CDATA[<?xml version=\"1.0\" encoding=\"gb2312\"?><paras><UserGuid>%@</UserGuid><KeyWord>%@</KeyWord><CurrentPageIndex>%@</CurrentPageIndex><PageSize>%@</PageSize><CategoryGuid>%@</CategoryGuid></paras>]]>";
    
    keyword = [EpointStringUtil filterXmlString:keyword];
    ParasXml = [NSString stringWithFormat:ParasXml,userguid,keyword,CurrentPageIndex,pagesize,categoryguid];
    
    
    
    //请求接口
    EpointWebServiceUtil *ws = [[EpointWebServiceUtil alloc]initWithUrl:url namespace:wsNamespace method:@"WebInfo_GetView"];
    [ws addParameterForString:@"ValidateData" value:ValidateData];
    [ws addParameterForString:@"ParasXml" value:ParasXml];
    NSString *bs =  ws.start;
    
    if ([self validateXMLWS:bs]) {
        
        [self exeSuccessWithData:[self getLst:bs]];
    }
}

-(void)setList:(NSMutableArray*)dataArray
{
    sqlite3 *cachedb = [[EpointCacheDB alloc]getCacheDB];
    char *errorMsg;
    const char *begin = "BEGIN TRANSACTION", *end = "COMMIT";
    sqlite3_stmt *beginStmt = NULL, *endStmt = NULL;
    
    sqlite3_prepare_v2(cachedb, begin, strlen(begin), &beginStmt, NULL);
    sqlite3_step(beginStmt);
    sqlite3_finalize(beginStmt);
    
    for (int i=0; i<dataArray.count; i++) {
        EpointWebInfoModel *m = [dataArray objectAtIndex:i];
        m.Content = [m.Content stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
        m.txtTitle = [m.txtTitle stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
        sqlite3_exec(cachedb, [[NSString stringWithFormat:@"delete from webinfocache where InfoGuid = '%@'",m.InfoGuid] UTF8String], NULL, NULL, &errorMsg);
        NSString *insertSql = [NSString stringWithFormat:@"insert into webinfocache values('%@','%@','%@','%@','%@','%@','%@',%@,'%@')",m.InfoGuid,m.txtTitle,m.Content,m.PostUserName,m.PostDate,m.InfoProperty,m.IsRead,m.HasAttach,m.FeedBackCount];
        sqlite3_exec(cachedb, [insertSql UTF8String], NULL, NULL, &errorMsg);
    }
    sqlite3_prepare_v2(cachedb, end, strlen(end), &endStmt, NULL);
    sqlite3_step(endStmt);
    sqlite3_finalize(endStmt);
    sqlite3_close(cachedb);
    
    if (errorMsg!=NULL) {
        NSLog(@"ERR:%s",errorMsg);
    }
}


-(NSMutableArray *)getLst:(NSString *)bs
{
    NSString *xmldata = [EpointStringUtil getXMLFormatedAttributeBsWithTag:bs xmlTag:@"InfoList"];
    GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithXMLString:xmldata  options:0 error:nil];
    GDataXMLElement *rootElement = [doc rootElement];
    NSArray *arrays = [rootElement elementsForName:@"Info"];
    NSMutableArray *listmp = [[NSMutableArray alloc]init];
    for (GDataXMLElement *arr in arrays)
    {
        EpointWebInfoModel *m = [[EpointWebInfoModel alloc]init];
        GDataXMLElement *InfoGuid = [[arr elementsForName:@"InfoGuid"] objectAtIndex:0];
        m.InfoGuid = [InfoGuid stringValue];
        
        GDataXMLElement *txtTitle = [[arr elementsForName:@"txtTitle"] objectAtIndex:0];
        m.txtTitle = [txtTitle stringValue];
        
        GDataXMLElement *Content = [[arr elementsForName:@"Content"] objectAtIndex:0];
        m.Content = [Content stringValue];
        
        GDataXMLElement *PostUserName = [[arr elementsForName:@"PostUserName"] objectAtIndex:0];
        m.PostUserName = [PostUserName stringValue];
        
        GDataXMLElement *PostDate = [[arr elementsForName:@"PostDate"] objectAtIndex:0];
        m.PostDate = [PostDate stringValue];
        
        GDataXMLElement *InfoProperty = [[arr elementsForName:@"InfoProperty"] objectAtIndex:0];
        m.InfoProperty = [InfoProperty stringValue];
        
        GDataXMLElement *IsRead = [[arr elementsForName:@"IsRead"] objectAtIndex:0];
        m.IsRead = [IsRead stringValue];
        
        GDataXMLElement *HasAttach = [[arr elementsForName:@"HasAttach"] objectAtIndex:0];
        m.HasAttach = [HasAttach stringValue];
        
        GDataXMLElement *FeedBackCount = [[arr elementsForName:@"FeedBackCount"] objectAtIndex:0];
        m.FeedBackCount = [FeedBackCount stringValue];
        
        [listmp addObject:m];
    }
    
    return listmp;
}

@end
